
//
 
#import "NT_CreatWaitView.h"

//
@interface NT_CreatWaitView ()<UITextViewDelegate>
/** view*/
@property (nonatomic, strong) UIView *whiteView;
/** shijian*/
@property (nonatomic, strong) UIButton *dateBtn;
/** shanchu*/
@property (nonatomic, strong) UIButton *deleteBtn;
/** <#Description#>*/
@property (nonatomic, strong) UITextView *waitTextView;
/** <#Description#>*/
@property (nonatomic, strong) UILabel *waitPlaceholderLabel; // 输入框提示语句
/** <#Description#>*/
@property (nonatomic, strong) UIButton *saveBtn;
//
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIDatePicker *waitDatePicker;
/** <#Description#>*/
@property (nonatomic, strong) UIView *topView;
/** <#Description#>*/
@property (nonatomic, strong) UIButton *cancelButton;
/** <#Description#>*/
@property (nonatomic, strong) UIButton *sureButton;
/** <#Description#>*/
@property (nonatomic, strong) NT_CoreDataBase *data;
/** 时间*/
@property (nonatomic, copy) NSString *dateStr;

@end

@implementation NT_CreatWaitView

- (instancetype)init {
    if ([super init]) {
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        self.frame = CGRectMake(0, kMainScreenHeight, kMainScreenWidth, kMainScreenHeight);
        _data = [[NT_CoreDataBase alloc]initWithCoreData:@"WaitData" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) { }];
        
        [self addSubview:self.whiteView];
        [self.whiteView addSubview:self.dateBtn];
        [self.whiteView addSubview:self.deleteBtn];
        [self.whiteView addSubview:self.waitTextView];
        [self.waitTextView addSubview:self.waitPlaceholderLabel];
        [self.whiteView addSubview:self.saveBtn];
        //        [self.whiteView addSubview:self.pickerDate];
        //
        [self.whiteView addSubview:self.contentView];
        [self.contentView addSubview:self.waitDatePicker];
        [self.contentView addSubview:self.topView];
        [self.topView addSubview:self.cancelButton];
        [self.topView addSubview:self.sureButton];
    }
    return self;
}

//
- (UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, kMainScreenHeight, kMainScreenWidth, 0)];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView;
}

- (UIButton *)dateBtn {
    if (!_dateBtn) {
        //当前时间
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        _dateStr = [formatter stringFromDate:[NSDate date]];
        //
        _dateBtn = [[UIButton alloc]initWithFrame:CGRectMake(kRelativeWidth(30), kRelativeHeight(20), kRelativeWidth(200), kRelativeHeight(20))];
        [_dateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _dateBtn.titleLabel.font = FontPFRegular(kRelativeWidth(16));
        [_dateBtn setTitle:_dateStr forState:UIControlStateNormal];
        [_dateBtn addTarget:self action:@selector(showdate) forControlEvents:UIControlEventTouchUpInside];
        _dateBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_dateBtn setImage:[UIImage imageNamed:@"arrow_down1"] forState:UIControlStateNormal];
        [_dateBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -_dateBtn.imageView.bounds.size.width, 0, _dateBtn.imageView.bounds.size.width)];
        [_dateBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _dateBtn.titleLabel.bounds.size.width, 0, -_dateBtn.titleLabel.bounds.size.width)];
        
    }
    return _dateBtn;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(kMainScreenWidth - kRelativeWidth(50), kRelativeHeight(15), kRelativeWidth(30), kRelativeHeight(30))];
        [_deleteBtn setImage:[UIImage imageNamed:@"delete2"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(ActionDele) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (UITextView *)waitTextView {
    if (!_waitTextView) {
        _waitTextView = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.dateBtn.frame), CGRectGetMaxY(self.dateBtn.frame)+kRelativeHeight(10), kMainScreenWidth - kRelativeWidth(60), kRelativeHeight(150))];
        _waitTextView.font = FontPFRegular(kRelativeWidth(15));
        _waitTextView.textColor = RGB(99, 99, 99);
        _waitTextView.delegate  = self;
    }
    return _waitTextView;
}
- (UILabel *)waitPlaceholderLabel {
    if (!_waitPlaceholderLabel) {
        _waitPlaceholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(2, 5, kRelativeWidth(200), kRelativeHeight(20))];
        _waitPlaceholderLabel.textColor = rgba(187, 187, 187, 1);
        _waitPlaceholderLabel.font = FontPFRegular(kRelativeWidth(14));
        _waitPlaceholderLabel.text = @"Input a content...";
    }
    return _waitPlaceholderLabel;
}
- (UIButton *)saveBtn {
    if (!_saveBtn) {
        _saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(kMainScreenWidth/2 - kRelativeWidth(30), CGRectGetMaxY(self.waitTextView.frame)+kRelativeHeight(20), kRelativeWidth(60), kRelativeHeight(30))];
        [_saveBtn setTitle:@"Save" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_saveBtn.titleLabel setFont:FontPFRegular(kRelativeWidth(15))];
        [_saveBtn addTarget:self action:@selector(ActionSave) forControlEvents:UIControlEventTouchUpInside];
        _saveBtn.layer.cornerRadius = 6;
        _saveBtn.layer.masksToBounds = YES;
        _saveBtn.layer.borderColor = [UIColor blueColor].CGColor;
        
    }
    return _saveBtn;
}

//MARK:时间picker
- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kRelativeHeight(300))];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.hidden = YES;
    }
    return _contentView;
}
- (UIDatePicker *)waitDatePicker
{
    if (!_waitDatePicker) {
        _waitDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kRelativeHeight(20), kMainScreenWidth, kRelativeHeight(260))];
        _waitDatePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_waitDatePicker addTarget:self
                            action:@selector(getWaitTime:)
                  forControlEvents:UIControlEventValueChanged];
        _waitDatePicker.datePickerMode = UIDatePickerModeDateAndTime;
    }
    return _waitDatePicker;
}
- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kRelativeHeight(40))];
        _topView.backgroundColor = UIColorFromHex(0x9A32CD);
    }return _topView;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 60, 30)];
        [_cancelButton setTitle:@"cancel" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
        
    }return _cancelButton;
}
- (UIButton *)sureButton {
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]initWithFrame:CGRectMake(kMainScreenWidth - 60, 5, 50, 30)];
        [_sureButton setTitle:@"sure" forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_sureButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }return _sureButton;
}


//
-(void)textViewDidChange:(UITextView *)textView{
    //清空内容
    _waitPlaceholderLabel.text = @"";
    if (_waitTextView.text.length == 0) {
        _waitPlaceholderLabel.text = @"Input a content...";
    }
    if ([textView.text length] > 500) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 500)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
}

//展示
- (void)showdate{
    _contentView.hidden = NO;
}
//MARK:日期回调
- (void)getWaitTime:(UIDatePicker *)datePicker {
    //
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    _dateStr = [formatter stringFromDate:datePicker.date];
    //   DLog(@"time===%@",dateStr)
    //    _dateStr = dateStr;
    [_dateBtn setTitle:_dateStr forState:UIControlStateNormal];
    //
    [_dateBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -_dateBtn.imageView.bounds.size.width, 0, _dateBtn.imageView.bounds.size.width)];
    [_dateBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _dateBtn.titleLabel.bounds.size.width, 0, -_dateBtn.titleLabel.bounds.size.width)];
    
}
- (void)leftButtonAction
{
    self.contentView.hidden = YES;
}


- (void)rightButtonAction
{
    self.contentView.hidden = YES;
}
//MARK:
- (void)ActionDele {
    [self remove];
}
//MARK:保存
- (void)ActionSave {
    if (_waitTextView.text.length <= 0) {
        return;
    }
    [self remove];
    [self.waitTextView resignFirstResponder];
    
    //
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[self randomwwwwww] forKey:@"id"];
    [dic setObject:_waitTextView.text forKey:@"title"];
    [dic setObject:_dateStr forKey:@"date"];
    //
    //
    __weak typeof(self) weakSelf = self;
    [_data ntInsertNewEntity:dic success:^{
        if (weakSelf.reloadWaitBlock) {
            weakSelf.reloadWaitBlock();
        }
    } fail:^(NSError * _Nullable error) {
    }];
    
}
//MARK:show
- (void)showWaitView {
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight);
        self.whiteView.frame = CGRectMake(0, CGRectGetMaxY(self.frame)-kRelativeHeight(300), kMainScreenWidth, kRelativeHeight(300));
    } completion:^(BOOL finished) {
    }];
}
- (void)remove{
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.frame = CGRectMake(0, kMainScreenHeight, kMainScreenWidth, kMainScreenHeight);
    } completion:^(BOOL finished) {
        [self remove];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self remove];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (NSString *)randomwwwwww
{
    const int count =6;
    NSString * sourceString=@"127893456";
    NSMutableString *reeeee=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[sourceString length];
        NSString * s=[sourceString substringWithRange:NSMakeRange(index, 1)];
        [reeeee appendString:s];
    }
    return reeeee;
}

@end
